<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: DictController.php 197 2011-02-18 12:45:33Z beimuaihui $
 */



class Dict_DictController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'Dict' );
		$this->dict = new Dict ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		$params=$this->getRequest()->getParams();
		$form_url=$params;
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['dict_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;		
		$cm['dict_name']=array("sortable"=>true);
		$cm['model_id']=array("hidden"=>true);
		$cm['model_name']=array("sortable"=>false);
		$cm['filter']=array("sortable"=>false);
		$cm['sorting']=array("sortable"=>false);
		$cm['ordering']=array("sortable"=>true);	
		$cm['state']=array("sortable"=>true);
		//$cm['action']=array("sortable"=>false);
		//$cm['ordering']=array("sortable"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete','model_detail'));
	//$grid->registerButton("fields","window.location.href='".BAOGG_BASE_URL."dict/dictfield/list/';");
		
		$this->view->form_url=BAOGG_BASE_URL."dict/dict/save";
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));
		
		$Model=new Model();
		$store_model=$Model->getCombo(array("state"=>1),array("sort"=>"ordering","dir"=>"asc"),null,array("model_id","model_name"));
		
		$form->addItem(0,array('name'=>'dict_name'));
		$form->addItem(2,array('name'=>'model_id','xtype'=>'combo','store'=>$store_model));
		$form->addItem(0,array('name'=>'filter'));
		$form->addItem(0,array('name'=>'sorting'));
		$form->addItem(1,array('name'=>'ordering'));
		$form->addItem(2,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));		
		$form->addItem(2,array('name'=>'dict_id',"xtype"=>"hidden","hideLabel"=>true));
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		
		//$form->addExistButtons(array('search','add','edit','delete','remove','reset'),array('edit','delete'));
		

		$this->view->form=$form;
		
		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		global $LANG;
	    $post=$this->_request->getPost ();
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="dict_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   
	   if(!$order){
	   	$order=array('sort'=>'ordering','dir'=>'asc');
	   }
	   
	   $rs=$this->dict->getList($where,$order,$limit);
	   $Model=new Model();
	   foreach((array)$rs as $k=>$v){
	   	$row_model=$Model->getOne($v['model_id']);
	   	$rs[$k]['model_name']=$row_model[0]['model_name'];
	   }
	   
	    $rs_cnt=$this->dict->getList($where,NULL,NULL,"count(*) as cnt");
		$this->view->grid = Zend_Json::encode(array("count"=>$rs_cnt[0]['cnt'],"data"=>$rs));
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}


	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->dict->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
	}
	
	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
		$arr=$this->dict->filterForm($formData);
        $action=$this->getRequest()->getParam('action');
		$rs = $this->dict->getSame($arr);
		if($action=='edit'){
			if(count($rs)==0 || count($rs)==1 &&  $rs[0] == $arr["dict_id"] ){
			    $this->dict->_update($arr,$arr["dict_id"]);
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["dict_id"]);
				$ret['success'] = $this->dict->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
		
	}

}
